package demo2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Company root = new ConcreteCompany("北京总公司");
        root.Add(new HRDepartment("总公司人力资源部"));
        root.Add(new FinanceDepartment("总公司财务部"));

        Company company2 = new ConcreteCompany("上海华东分公司");
        company2.Add(new HRDepartment("上海分公司人力资源部"));
        company2.Add(new FinanceDepartment("上海分公司财务部"));
        root.Add(company2);

        Company company3 = new ConcreteCompany("南京分公司");
        company3.Add(new HRDepartment("南京分公司人力资源部"));
        company3.Add(new FinanceDepartment("南京分公司财务部"));
        root.Add(company3);

        root.Display();
        root.LineOfDuty();
    }
}

abstract class Company{
    protected String name;

    public Company(String name){
        this.name = name;
    }

    public abstract void Add(Company c);
    public abstract void Remove(Company c);
    public abstract void Display();
    public abstract void LineOfDuty(); // 履行职责
}

class ConcreteCompany extends Company{
    //具体公司类

    private List<Company> children = new ArrayList<Company>();
    public ConcreteCompany(String name) {
        super(name);
    }

    @Override
    public void Add(Company c) {
        children.add(c);
    }

    @Override
    public void Remove(Company c) {
        children.remove(c);
    }

    @Override
    public void Display() {
        System.out.println("支: " +name);
        for (Company company:children
             ) {
            company.Display();
        }
    }

    @Override
    public void LineOfDuty() {
        for (Company company:children
             ) {
            company.LineOfDuty();
        }
    }
}

class HRDepartment extends Company{
    //人事部


    public HRDepartment(String name) {
        super(name);
    }

    @Override
    public void Add(Company c) {

    }

    @Override
    public void Remove(Company c) {

    }

    @Override
    public void Display() {
        System.out.println("叶: " +name);
    }

    @Override
    public void LineOfDuty() {
        System.out.println("员工培训管理: " +name);
    }
}

class FinanceDepartment extends Company{
    //财务部

    public FinanceDepartment(String name) {
        super(name);
    }

    @Override
    public void Add(Company c) {

    }

    @Override
    public void Remove(Company c) {

    }

    @Override
    public void Display() {
        System.out.println("叶: " +name);
    }

    @Override
    public void LineOfDuty() {
        System.out.println("公司财务收支管理: "+name);
    }
}